import * as actions from '../actions/index';

// 对页面prop 数据进行管理
const initialState = {
  tabKey: 'opened',
  orderKey: 'updated_at',
  pageSize: 10,
  search: '',
  mergeRequests: [],
  totalData: 0,
  currentPage: 1,
  isLoading: true,
  advancedSearchData: {},
  project_clone_download: {},
  ipSetting: undefined,
};
const defaultAction = {
  type: 'doNothing',
};

export default function index(state = initialState, action = defaultAction) {
  switch (action.type) {
    case actions.UPDATE_DATA: {
      return { ...state, ...action.data };
    }
    case actions.RECEIVE_DATA: {
      return {
        ...state,
        mergeRequests: [...action.data],
        currentPage: action.data.currentPage,
        isLoading: false,
      };
    }
    case actions.RECEIVE_TOTAL_DATA: {
      return { ...state, totalData: action.data };
    }
    case actions.SET_SEARCH:
      return { ...state, search: action.data };
    case actions.SET_ADVANCEDSEARCHDATA:
      return { ...state, advancedSearchData: action.data, tabKey: action.data.state };
    case actions.SET_TAB_KEY:
      return {
        ...state,
        tabKey: action.data,
        advancedSearchData: { ...state.advancedSearchData, state: action.data },
      };
    case actions.SET_ORDER_KEY:
      return { ...state, orderKey: action.data };
    case actions.SET_RESET_LIST:
      return { ...state, mergeRequests: [], currentPage: 1, isLoading: true };
    case actions.SET_INIT_LIST:
      return { ...state, ...initialState };
    case actions.SET_PROJECT_CLONE_DOWNLOAD:
      return { ...state, project_clone_download: action.data };
    case actions.GET_IP_WHITE:
      return { ...state, ipSetting: action.data };
    default:
      return state;
  }
}
